package com.xul.controller;

import com.xul.entity.Clazz;
import com.xul.entity.Subject;
import com.xul.service.ClazzService;
import com.xul.service.ClazzService;
import com.xul.service.SubjectService;
import com.xul.utils.MapController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author xul
 * @create 2021-07-02 15:56
 */

@Controller
@RequestMapping("clazz")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    @Autowired
    private SubjectService subjectService;

    private final String LIST = "clazz/list";
    private final String ADD = "clazz/add";
    private final String UPDATE = "clazz/update";


    @GetMapping("add")
    public String add(ModelMap modelMap) {
        List<Subject> subjects = subjectService.query(null);
        modelMap.addAttribute("subjects", subjects);
        return ADD;
    }

    @PostMapping("create")
    @ResponseBody
    public Map<String, Object> create(Clazz clazz) {
        int result = clazzService.create(clazz);
        if (result <= 0) {
            return MapController.getInstance().error().getMap();
        }
        return MapController.getInstance().success().getMap();
    }

    @PostMapping("delete/{id}")
    @ResponseBody
    public Map<String, Object> delete(@PathVariable("id") Integer id) {
        int result = clazzService.delete(id);
        if (result <= 0) {
            return MapController.getInstance().error().getMap();
        }
        return MapController.getInstance().success().getMap();
    }

    @PostMapping("delete")
    @ResponseBody
    public Map<String, Object> delete(String ids) {
        int result = clazzService.delete(ids);
        if (result <= 0) {
            return MapController.getInstance().error().getMap();
        }
        return MapController.getInstance().success().getMap();
    }

    @PostMapping("update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody Clazz clazz) {
        int result = clazzService.update(clazz);
        if (result <= 0) {
            return MapController.getInstance().error().getMap();
        }
        return MapController.getInstance().success().getMap();
    }

    @GetMapping("detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap modelMap) {
        Clazz clazz = clazzService.detail(id);
        List<Subject> subjects = subjectService.query(null);
        modelMap.addAttribute("clazz", clazz);
        modelMap.addAttribute("subjects", subjects);
        return UPDATE;
    }


    @PostMapping("query")
    @ResponseBody
    public Map<String, Object> query(@RequestBody Clazz clazz) {
        List<Clazz> list = clazzService.query(clazz);
        List<Subject> subjects = subjectService.query(null);
        // 循环赋值
        list.forEach(entity -> {
            subjects.forEach(subject -> {
                if (entity.getSubjectId() == subject.getId().intValue()) {
                    entity.setSubject(subject);
                }
            });
        });
        Integer count = clazzService.count(clazz);
        return MapController.getInstance().success().put("data", list).put("count", count).getMap();
    }

    @GetMapping("list")
    public String list() {
        return LIST;
    }
}
